package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import com.sky.service.SetMealService;
import com.sky.service.impl.CategoryServiceImpl;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
@Api("套餐相关接口")
@Slf4j
public class SetMealController {

    @Autowired
    SetMealService setMealService;


    @PostMapping
    @ApiOperation("新增套餐")
    @CacheEvict(cacheNames = "setmealCache", key = "#setmealDTO.categoryId")
    public Result save(@RequestBody SetmealDTO setmealDTO) {
        log.info("套餐信息:{}", setmealDTO);
        setMealService.save(setmealDTO);
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("套餐分页查询")
    public Result<PageResult> PageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        log.info("套餐分页查询:{}", setmealPageQueryDTO);
        //调用查询方法返回总记录数 和当前页数
        PageResult pageResult = setMealService.PageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    @DeleteMapping
    @ApiOperation("套餐删除")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true) //删除所有缓存数据
    public Result DeleteSetmeal(@RequestParam List<Long> ids) {
        log.info("删除套餐信息:{}", ids);
        setMealService.Delete(ids);

        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据ID查询套餐")
    @CacheEvict(cacheNames = "setmealCache", key = "'id'")
    public Result<SetmealVO> GetSetmealById(@PathVariable Long id) {
        log.info("套餐id:{}", id);

        SetmealVO setmealVO = setMealService.getByIdWithDish(id);

        return Result.success(setmealVO);

    }

    @PutMapping
    @ApiOperation("修改套餐")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTOe) {
        log.info("套餐信息:{}", setmealDTOe);

        setMealService.Update(setmealDTOe);

        return Result.success();
    }


    @PostMapping("/status/{status}")
    @ApiOperation("套餐状态跟新")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result UpdateStatus(@PathVariable Integer status, Long id) {
        log.info("跟新状态:{}", status);
        setMealService.UpdateStatus(status, id);


        return Result.success();
    }

}
